﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Xml.Linq;
using System;
using System.IO;
using System.Windows.Input;

namespace MidwayBattle
{
    /// <summary>
    /// Logique d'interaction pour WindowEditPalyer.xaml
    /// </summary>
    public partial class WindowEditPlayer : Window
    {
        private string Pass;
        private MainWindow Mw;

        public WindowEditPlayer(MainWindow Mw)
        {
            InitializeComponent();
            this.MinHeight = 350;
            this.MinWidth = 350;
            this.Mw = Mw;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBox.Show("A file with your password has been generated on your desktop", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                string text = "Your password : " + Pass;
                string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                File.WriteAllText(mydocpath + @"\YourPassword.txt", text);
            }
            catch (Exception Iox)
            {
                MessageBox.Show(Iox.Message, "Edition player", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                XDocument doc = XDocument.Load("Users.xml");
                string Lg = this.textBox1.Text.ToUpper();
                string Pwd = this.passwordBox1.Password;
                IEnumerable<XElement> rech =
                         from el in doc.Root.Elements("Users")
                         where (string)el.Element("UserLog") == Lg
                         select el;

                if (rech.Count() != 0)
                {
                    foreach (XElement el in rech)
                    {
                        if ((string)el.Element("UserPassword") == Pwd)
                        {
                            string NewLg = this.textBox2.Text.ToUpper();
                            string NewPwd = this.passwordBox2.Password;
                            string ConfNewPwd = this.passwordBox3.Password;

                            if (NewLg != "Pseudo")
                            {
                                IEnumerable<XElement> req =
                                from dl in doc.Root.Elements("Users")
                                where (string)dl.Element("UserLog") == NewLg
                                select dl;

                                if (req.Count() != 0)
                                {
                                    MessageBox.Show("The new login already exist", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                                    this.textBox2.Text = "Pseudo";

                                }
                                else
                                {
                                    if (NewPwd.Length < 6)
                                    {
                                        MessageBox.Show("New password too short or empty", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                                        this.passwordBox2.Password = "********";
                                        this.passwordBox3.Password = "+-+-+-+-";
                                    }
                                    else
                                    {
                                        int i;
                                        for (i = 0; i < NewPwd.Length && NewPwd[i] != ' '; i++) ;

                                        if (i < NewPwd.Length)
                                        {
                                            MessageBox.Show("The new password can not include spaces", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                                            this.passwordBox2.Password = "********";
                                            this.passwordBox3.Password = "+-+-+-+-";
                                        }
                                        else
                                        {
                                            if (ConfNewPwd != NewPwd)
                                            {
                                                MessageBox.Show("New passwords do not match", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                                                this.passwordBox2.Password = "********";
                                                this.passwordBox3.Password = "+-+-+-+-";
                                            }
                                            else
                                            {
                                                el.Element("UserLog").SetValue(NewLg);
                                                el.Element("UserPassword").SetValue(NewPwd);
                                                doc.Save("Users.xml");
                                                MessageBox.Show("The player has been successfully changed", "Edition player", MessageBoxButton.OK, MessageBoxImage.Information);
                                                this.Close();
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                MessageBox.Show("The login <Pseudo> is not valid", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                            }


                        }
                        else
                        {
                            Pass = (string)el.Element("UserPassword");
                            this.button3.Visibility = Visibility;
                            MessageBox.Show("Incorrect password", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Incorrect login", "Edition player", MessageBoxButton.OK, MessageBoxImage.Warning);
                    this.textBox1.Text = "Pseudo";
                }
            }
            catch (System.IO.FileNotFoundException ex)
            {
                MessageBox.Show(ex.Message + " A new file will be created but you will have to create a new profile.", "Edition player", MessageBoxButton.OK, MessageBoxImage.Error);
            }


        }

        private void Clear_GotMouseCapture(object sender, MouseEventArgs e)
        {
            if (sender.Equals(textBox2))
            {
                this.textBox2.Clear();
            }
            else
            {
                if (sender.Equals(passwordBox1))
                {
                    this.passwordBox1.Clear();
                }
                else
                {
                    if (sender.Equals(passwordBox2))
                    {
                        this.passwordBox2.Clear();
                    }
                    else
                    {
                        if (sender.Equals(passwordBox3))
                        {
                            this.passwordBox3.Clear();
                        }
                        else
                        {
                            this.textBox1.Clear();
                        }
                    }

                }
            }
        }
    }
}
